//
//  userController.m
//  uivc
//
//  Created by 包子 on 12-8-10.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "UserController.h"
#import "Comm.h"
#import "UserLogin.h"
#import "UserPasswordUpdate.h"
#import "UserPrepay.h"
#import "UserOrder.h"
#import "UserFavorite.h"
#import "UserAddress.h"

@implementation UserController


#pragma mark -
#pragma mark Initialization

/*
 - (id)initWithStyle:(UITableViewStyle)style {
 // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
 self = [super initWithStyle:style];
 if (self) {
 // Custom initialization.
 }
 return self;
 }
 */

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];
	
	comm = [Comm instance];
	//添加注销按钮
	UIBarButtonItem *bt_unLogin = [[UIBarButtonItem alloc] initWithTitle:@"退出登录" style:UIBarButtonItemStylePlain target:self action:@selector(unLogin)];
	self.navigationItem.rightBarButtonItem = bt_unLogin;
	[bt_unLogin release];	
	
	
	self.navigationItem.title = @"用户中心";	
}

-(void)unLogin{
	[[comm user] unLogin];
	UserLogin * viewController  = [[UserLogin alloc] init];
	[self.navigationController pushViewController:viewController animated:YES];
	
	[viewController release];
}

/*
 - (void)viewWillAppear:(BOOL)animated {
 [super viewWillAppear:animated];
 }
 */

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
	//检查是否登录
	if ([[comm user] isLogin] ) {
		//填充用户信息
		NSLog(@"login");
	}else {
		//跳转到登录界面
		NSLog(@"退出登录");
		UserLogin *viewController = [[UserLogin alloc] init];
		[self.navigationController pushViewController:viewController animated:NO];
		[viewController release];
	}
	
}



#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *CellIdentifier = @"Cell";
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
	int row = indexPath.row;
	
	if (indexPath.section == 0) {	
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
		
		switch (row) {
			case 0:
				cell.imageView.image = [UIImage imageNamed:@"icon_home.png"];
				cell.textLabel.text = @"用户名" sp;
				cell.detailTextLabel.text = [[comm user] userCode];
				break;
			case 1:
				cell.imageView.image = [UIImage imageNamed:@"icon_user.png"];
				cell.textLabel.text = @"昵称" sp;
				cell.detailTextLabel.text = [[comm user] name];
				break;
			case 2:
				cell.imageView.image = [UIImage imageNamed:@"music.png"];
				cell.textLabel.text = @"余额" sp;
				cell.detailTextLabel.text = [[comm user] balance];
				break;
			case 3:
				cell.imageView.image = [UIImage imageNamed:@"detail.png"];
				cell.textLabel.text = @"积分" sp;
				cell.detailTextLabel.text = [[comm user] rewards];
				break;
			case 4:
				cell.imageView.image = [UIImage imageNamed:@"detail.png"];
				cell.textLabel.text = @"电话" sp;
				cell.detailTextLabel.text = [[comm user] phone];
				break;
			default:
				break;
		}
		
		cell.detailTextLabel.numberOfLines = 1;
		cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
		cell.detailTextLabel.textColor = [UIColor redColor];
		
		return cell;
	}else {
		
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
		
		switch (row) {
			case 0:
				cell.imageView.image = [UIImage imageNamed:@"icon_home.png"];
				cell.textLabel.text = @"我的订单";
				break;
			case 1:
				cell.imageView.image = [UIImage imageNamed:@"icon_user.png"];
				cell.textLabel.text = @"我的收藏";
				break;
			case 2:
				cell.imageView.image = [UIImage imageNamed:@"music.png"];
				cell.textLabel.text = @"会员充值";
				break;
			case 3:
				cell.imageView.image = [UIImage imageNamed:@"detail.png"];
				cell.textLabel.text = @"地址管理";
				break;
			case 4:
				cell.imageView.image = [UIImage imageNamed:@"detail.png"];
				cell.textLabel.text = @"密码修改";
				break;
			default:
				break;
		}
		
		return cell;
	}
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {	
    if (indexPath.section == 1) {        
        UserOrder * uo;
        UserFavorite * uf;
        UserPrepay * up;
        UserAddress * ua;
        UserPasswordUpdate *upu;
        
        switch (indexPath.row) {
            case 0:
                uo = [[UserOrder alloc] init];
                [self.navigationController pushViewController:uo animated:YES];
                [uo release];
                break;
            case 1:
                uf = [[UserFavorite alloc] init];
                [self.navigationController pushViewController:uf animated:YES];
                [uf release];
                break;
            case 2:
                up = [[UserPrepay alloc] init];
                [self.navigationController pushViewController:up animated:YES];
                [up release];
                break;
            case 3:
                ua = [[UserAddress alloc] init];
                [self.navigationController pushViewController:ua animated:YES];
                [ua release];
                break;
            case 4:
                upu = [[UserPasswordUpdate alloc] init];
                [self.navigationController pushViewController:upu animated:YES];
                [upu release];
                break;
            default:
                break;
        }        
    }
    
}


#pragma mark -
#pragma mark Table view delegate

- (void)viewWillAppear:(BOOL)animated{
	
}

#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
	[super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
	//Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
	// For example: self.myOutlet = nil;
}


- (void)dealloc {
	[comm release];
	[super dealloc];
}


@end

